package com.ldj.taglib;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import com.ldj.hibernate.DataDictionary;
import com.ldj.hibernate.DataDictionaryDAO;

public class SelectAllTag extends TagSupport{

	private static final long serialVersionUID = 1L;
	
	private String name;
	
	private String key;
	
	private String styleClass;
	
	private String firstValue;
	
	private String firstTitle;
	
	private String dataType;

	public int doStartTag() throws JspException{
		try{
			if(key == null)
				key = "";
			StringBuffer stringbuffer = new StringBuffer("");
			StringBuffer scriptbuffer = new StringBuffer("");
			
			scriptbuffer.append("<script>\n");
			scriptbuffer.append("function sbt" + name + "(value, obj){\n");
			scriptbuffer.append("document.getElementById(\"" + name + "\").value=value;\n");
			scriptbuffer.append("var sel = document.getElementById(obj);\n");
			scriptbuffer.append("if(sel == null)\n");
			scriptbuffer.append("return;\n");
			scriptbuffer.append("for(var k = 0; k < sel.options.length; k ++){\n");
			scriptbuffer.append("if(k >= 0 && k <= sel.options.length-1){\n");
			scriptbuffer.append("sel.options[k] = null;\n");
			scriptbuffer.append("k --;\n");
			scriptbuffer.append("}\n");
			scriptbuffer.append("}\n");
			
			HashMap map = getLevel1Code(dataType);
			stringbuffer.append((String)map.get("str"));
			stringbuffer.append("<input type=\"hidden\" id=\"" + name + "\" name=\"" + name + "\" value=\"" + key + "\">");
			List dataCodeList = (List)map.get("dataCodeList");
			if(dataCodeList != null && dataCodeList.size() > 0){
				if(firstTitle != null && !"".equals(firstTitle)){
					if(firstValue == null)
						firstValue = "";
					scriptbuffer.append("opt = new Option(\"" + firstTitle + "\",\"" + firstValue + "\");\n");
					scriptbuffer.append("sel.options.add(opt);\n");
				}
				for(int i = 0; i < dataCodeList.size(); i ++){
					String code = (String)dataCodeList.get(i);
					scriptbuffer.append("if(value == \"" + code + "\"){\n");
					scriptbuffer.append("var opt = null;\n");
					List dList = (List)map.get(code);
					if(dList != null && dList.size() > 0){
						for(int j = 0; j < dList.size(); j ++){
							DataDictionary d = (DataDictionary)dList.get(j);
							scriptbuffer.append("opt = new Option(\"" + d.getDataDictionaryName() + "\",\"" + d.getDataDictionaryCode() + "\");\n");
							scriptbuffer.append("sel.options.add(opt);\n");
						}
					}
					scriptbuffer.append("}\n");
				}
			}
			scriptbuffer.append("}\n");
			scriptbuffer.append("</script>\n");
			pageContext.getOut().write(stringbuffer.toString() +  scriptbuffer.toString());
		}catch (IOException ex){
			throw new JspTagException("acmstag: " + ex.getMessage());
		} catch(Exception ex){
			throw new JspTagException("acmstag: " + ex.getMessage());
		}
		return SKIP_BODY;
	}
	
	public HashMap getLevel1Code(String value){
		String stringbuffer = "";
		DataDictionaryDAO dao = new DataDictionaryDAO(null, null);
		stringbuffer += "<select name=\"" + name + "0\"";
		if(styleClass != null && !"".equals(styleClass)){
			stringbuffer += " class=\"" + styleClass + "\"";
		}
		stringbuffer += " onchange=\"javascript:sbt" + name + "(this.value, '" + name + "1')\">\n";
		if(firstTitle != null && !"".equals(firstTitle)){
			if(firstValue == null)
				firstValue = "";
			stringbuffer += "<option value=\"" + firstValue + "\">" + firstTitle + "</option>\n";
		}
		List list = dao.findListFromCode(value);
		if(list != null && list.size() > 0){
			for(int i = 0; i < list.size(); i ++){
				DataDictionary d = (DataDictionary)list.get(i);
				stringbuffer += "<option value=\"" + d.getDataDictionaryCode() + "\"";
				if(key != null && key.startsWith(d.getDataDictionaryCode()))
					stringbuffer += " selected";
				stringbuffer += ">" + d.getDataDictionaryName() + "</option>\n";
			}
		}
		stringbuffer += "</select>\n";
		HashMap map = new HashMap();
		map.put("dataCodeList", new Vector());
		map = getLevel2Code(map, value + "___", 1);
		String str = (String)map.get("str");
		if(str.equals("") || str.equals("-")){
		}else{
			stringbuffer += str;
		}
		map.put("str", stringbuffer);
		return map;
	}
	
	public HashMap getLevel2Code(HashMap map, String value, int level){
		String stringbuffer = "";
		DataDictionaryDAO dao = new DataDictionaryDAO(null, null);
		List list = dao.findListFromCode(value);
		if(list != null && list.size() > 0){
			stringbuffer += "<select id=\"" + name + level + "\" name=\"" + name + level + "\"";
			if(styleClass != null && !"".equals(styleClass)){
				stringbuffer += " class=\"" + styleClass + "\"";
			}
			stringbuffer += " onchange=\"javascript:sbt" + name + "(this.value, '" + name + (level+1) + "')\">\n";
			if(firstTitle != null && !"".equals(firstTitle)){
				if(firstValue == null)
					firstValue = "";
				stringbuffer += "<option value=\"" + firstValue + "\">" + firstTitle + "</option>\n";
			}
			for(int i = 0; i < list.size(); i ++){
				DataDictionary d = (DataDictionary)list.get(i);
				String code = d.getDataDictionaryCode();
				String code1 = code.substring(0, code.length() - 3);
				if(map.containsKey(code1)){
					List dList = (List)map.get(code1);
					dList.add(d);
					map.put(code1, dList);
				}else{
					List dataCodeList = (List)map.get("dataCodeList");
					dataCodeList.add(code1);
					List dList = new Vector();
					dList.add(d);
					map.put(code1, dList);
				}
				if(key != null && key.startsWith(code1)){
					stringbuffer += "<option value=\"" + code + "\"";
					if(key != null && key.startsWith(d.getDataDictionaryCode()))
						stringbuffer += " selected";
					stringbuffer += ">" + d.getDataDictionaryName() + "</option>\n";
				}
			}
			stringbuffer += "</select>\n";
			map = getLevel2Code(map, value + "___", level + 1);
			String str = (String)map.get("str");
			if(str.equals("") || str.equals("-")){
			}else{
				stringbuffer += str;
			}
		}
		map.put("str", stringbuffer);
		return map;
	}

	public int doEndTag(){
		return EVAL_PAGE;
	}

	public String getFirstTitle() {
		return firstTitle;
	}

	public void setFirstTitle(String firstTitle) {
		this.firstTitle = firstTitle;
	}

	public String getFirstValue() {
		return firstValue;
	}

	public void setFirstValue(String firstValue) {
		this.firstValue = firstValue;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}
}
